package com.nervenets.shopping.hibernate.domain.user;

import com.nervenets.general.hibernate.DomainObject;
import com.nervenets.shopping.enumeration.*;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.Index;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;

/**
 * 账户流水
 * Created by Joe on 2018/1/6.
 */
@EqualsAndHashCode(callSuper = true)
@Entity
@Table(
        indexes = {
                @javax.persistence.Index(name = "index_of_user_wealth", columnList = "userId,wt"),
                @javax.persistence.Index(name = "index_of_user_wealth_time", columnList = "userId,wt,ct"),
                @javax.persistence.Index(name = "index_of_wealth_total", columnList = "wt,ot,ct")
        }
)
@Data
@NoArgsConstructor
@AllArgsConstructor
public class WealthFlow extends DomainObject {
    public static final String CACHE_KEY = "WEALTH";
    @Index(name = "_idx_user_wealth_flows_userId")
    @ApiModelProperty(value = "用户ID")
    private long userId;
    @ApiModelProperty(value = "变动值")
    private long offset;
    @ApiModelProperty(value = "结余")
    private long surplus;
    @ApiModelProperty(value = "累计值")
    private long total;
    @Column(name = "wt", columnDefinition = "tinyint")
    @ApiModelProperty(value = "资产类型")
    private WealthType wealthType;
    @Column(name = "ot", columnDefinition = "tinyint")
    @ApiModelProperty(value = "操作行为")
    private OperateType operateType;
    @Column(name = "fs", columnDefinition = "tinyint")
    @ApiModelProperty(value = "流水状态")
    private FlowStatus flowStatus = FlowStatus.success;
    @Column(name = "pt", columnDefinition = "tinyint")
    @ApiModelProperty(value = "支付类型")
    private PayType payType = PayType.surplus;
    @Column(name = "ut", columnDefinition = "tinyint")
    @ApiModelProperty(value = "关联类型")
    private UnionType unionType = UnionType.normal;
    @ApiModelProperty(value = "关联ID")
    private long unionId = 0;
    @ApiModelProperty(value = "操作IP")
    private long ip = 0;
    @ApiModelProperty(value = "流水说明")
    @Column(columnDefinition = "varchar(50)")
    private String detail;

    public WealthFlow(long userId, long offset, long surplus, OperateType operateType, long ip) {
        this.userId = userId;
        this.offset = offset;
        this.surplus = surplus;
        this.operateType = operateType;
        this.ip = ip;
    }

    public WealthFlow(long userId, long offset, long surplus, OperateType operateType, long ip, long unionId, UnionType unionType, String detail) {
        this(userId, offset, surplus, operateType, ip);
        this.unionType = unionType;
        this.unionId = unionId;
        this.detail = detail;
    }
}
